﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Medianamik.Core.Sugar;
using Medianamik.Globalization;
using Medianamik.UI.Controls;
using Medianamik.UI.Web.Forum;

namespace Medianamik.UI.Web.Forum
{
    public partial class ForumManagement : BaseModuleEditionPage<NodeWrapper>
    {
        #region Fields

        private const string _returnUrl = "forumlist.aspx";

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lnkCancel.NavigateUrl = ReturnURL;

                bctFilAriane.Breadcrumbs = GetBreadcrumbs();
                bctFilAriane.DataBind();
            }
        }

        protected void OnNavigate(object sender, HistoryEventArgs e)
        {
            if (MyScriptManager.IsNavigating)
            {
                string nodeId = e.State.Get("NodeId");

                string script = String.Format("if (confirm('{3}')) {{ $find('{4}').tryLock('{0}', 3600, function() {{ OnNavigate('{0}', '{1}'); }}, function() {{ __doPostBack('{2}', '{0}'); }});}}"
                        , nodeId
                        , backOrForward.ClientID
                        , lockResquestFailed.ClientID
                        , Translator.Translate("Medianamik_ContentManagement", "SwitchNodeEditionMessage")
                        , MedianamikFormTemplate.LockRequester.ClientID);


                ScriptManager.RegisterStartupScript(this, typeof(ForumManagement), "backOrForwardScript", script, true);
            }
        }

        #endregion

        #region Properties

        protected override string ReturnURL
        {
            get
            {
                //if (!ParentNodeId.HasValue ||
                //    ParentNodeId.Value == MedianamikModules.ForumManagement ||
                //    ParentNodeId.Value == Guid.Empty)
                //{
                //    return _returnUrl;
                //}

                return _returnUrl + "?parentid=" + CurrentNode.ParentID;
            }
        }

        public override MedianamikFormTemplate MedianamikFormTemplate
        {
            get { return MedFormTemplate; }
        }

        public override LinkButton SaveButton
        {
            get { return btnSave; }
        }

        protected override Guid? ParentNodeId
        {
            get
            {
                return base.ParentNodeId ?? MedianamikModules.ForumManagement;
            }
        }

        public override Guid? TypeId
        {
            get
            {
                if (ParentNode == null)
                    throw new ApplicationException("Unknown TypeId.");

                if (ParentNode.NodeTypeID == ForumTypes.ForumGroup)
                    return ForumTypes.Forum;

                if (ParentNode.NodeTypeID == ForumTypes.Forum)
                    return ForumTypes.ForumThread;

                return ParentNode.NodeTypeID == ForumTypes.ForumThread ? 
                    ForumTypes.ForumPost : ForumTypes.ForumGroup;
            }
        }

        #endregion

        #region Methods

        protected IEnumerable<Breadcrumb> GetBreadcrumbs()
        {
            //TODO:
            //yield return new Breadcrumb(RootNodeId.ToString(), 
            //    Translator.Translate("sitemap", ReturnURL));

            if (IsEdition)
                yield return new Breadcrumb(CurrentNode.ID.ToString(),
                    CurrentNode.DisplayName);
        }

        #endregion
    }
}
